package CacheKey;



import Utils.*;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Component
public class CacheKeyGenerator implements KeyGenerator {

    @Override
    public Object generate(Object target, Method method, Object... params) {
        // TODO Auto-generated method stub
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("package", target.getClass().getPackage());
        map.put("target", target.getClass().toGenericString());//放入target的名字
        map.put("method", method.getName());//放入method的名字
        if (params != null && params.length > 0) {//把所有参数放进去
            int i = 0;
            if (params.length == 2 && params[0] instanceof HttpServletRequest && params[1] instanceof HttpServletResponse) {
                Map<String, Object> map1 = RequestUtil.getParameterMap((HttpServletRequest) params[0]);
                if (map1 != null) {
                    map.putAll(map1);
                }

            }
        } else if (params.length == 2 && params[1] instanceof HttpServletRequest && params[0] instanceof HttpServletResponse) {
            Map<String, Object> map1 = RequestUtil.getParameterMap((HttpServletRequest) params[1]);
            if (map1 != null) {
                map.putAll(map1);
            }
        } else if (params.length == 1 && params[0] instanceof HttpServletRequest) {
            Map<String, Object> map1 = RequestUtil.getParameterMap((HttpServletRequest) params[0]);
            if (map1 != null) {
                map.putAll(map1);
            }
        } else {
            int i = 0;
            for (Object o : params) {

                if (!(o instanceof HttpServletRequest) && !(o instanceof HttpServletResponse)) {
                    if (o != null && o.toString().length() > 0 && !o.toString().equalsIgnoreCase("null")) {
                        i++;

                        if (SimpleTypeUtil.isSimpleValueType(o.getClass())) {
                            if (o instanceof Date) {

                                map.put("params-" + i,
                                        DateUtil.parseDateToStr((Date) o, DateUtil.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS));
                            } else {
                                map.put("params-" + i, o);
                            }
                        } else {
                            map.put("params-" + i, GsonUtil.GsonString(o).hashCode());
                        }
                    }
                }


            }
        }


        String str = GsonUtil.GsonString(map).toString();
        byte[] hash = null;
        String s = null;
        try {
            hash = MessageDigest.getInstance("MD5").digest(str.getBytes("UTF-8"));
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }

        try {
            s = MD5Encoder.encode(hash);//使用MD5生成位移key
        } catch (Exception e) {
            e.printStackTrace();
        }
        return s;

    }


}

